package com.letao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.letao.common.pojo.EUDataGridResult;
import com.letao.common.pojo.LetaoResult;
import com.letao.common.utils.IDUtils;
import com.letao.mapper.TbItemParamMapper;
import com.letao.pojo.TbItemParam;
import com.letao.pojo.TbItemParamExample;
import com.letao.service.ItemParamSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ac on 2017/1/11.
 */
@Service
public class ItemParamServiceImpl implements ItemParamSevice {
    @Autowired
    private TbItemParamMapper itemParamMapper;

    @Override
    public LetaoResult getItemParaByCid(long cid) {
        TbItemParamExample example = new TbItemParamExample();
        example.createCriteria().andItemCatIdEqualTo(cid);
        List<TbItemParam> params = itemParamMapper.selectByExampleWithBLOBs(example);
        if(params != null && params.size() > 0){
            return LetaoResult.ok(params.get(0));
        }else{
            return LetaoResult.ok();
        }
    }

    @Override
    public EUDataGridResult findItemParaList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(new TbItemParamExample());
        //获取分页信息
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(list);
        return new EUDataGridResult(pageInfo.getTotal(), list);
    }

    @Override
    public LetaoResult insertItemPara(TbItemParam tbItemParam) {
        tbItemParam.setId(IDUtils.genItemId());
        tbItemParam.setCreated(new Date());
        tbItemParam.setUpdated(new Date());
        itemParamMapper.insert(tbItemParam);
        return LetaoResult.ok();
    }
}
